package io.zhengsh.vvip.leetcode;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class BubbleSortTest {

    public static void main(String[] args) {

        List<int[]> list = new ArrayList<>();
        for (int j = 0; j < 1000; j++) {
            int[] arr = new int[1000];
            for (int i = 0; i < 1000; i++) {
                arr[i] = new Random().nextInt(1000);
            }
            list.add(arr);
        }

        long start = System.currentTimeMillis();
        for (int[] item : list) {
            bubbleSort(item);
        }
        System.out.println("cost time:" + (System.currentTimeMillis() - start) + "ms");
    }

    //按照刚才那个动图进行对应
    //冒泡排序两两比较的元素是没有被排序过的元素--->
    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {//控制比较轮次，一共 n-1 趟
            for (int j = 0; j < array.length - 1 - i; j++) {//控制两个挨着的元素进行比较
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
    }
}
